<template>
<!--登录表单的容器-->
  <div class="login_container">
<!--登录区域-->
    <div class="login_box">
<!--头像-->
      <div class="login_nav">
        <label>欢迎登录</label>
      </div>
<!--表单-->
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="login_Form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
    <div class="login_footer">
      <p>销售管理系统by17软卓2班陈继伟</p>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { USERLOGIN, USERLOGOUT } from '../store/types'
import { doLogin } from '../api/login'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 2, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    ...mapMutations([USERLOGIN, USERLOGOUT]),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await doLogin(this.loginForm)
          console.log(111)
          console.log(res)
          // 登录失败
          if (res.code !== 200) {
            return this.$message.error({
              showClose: true,
              message: res.message,
              center: true,
              duration: 1000
            })
          }
          // 登录成功
          this.$message.success({
            showClose: true,
            message: res.message,
            center: true,
            duration: 1000
          })
          // 保存token 用于权限验证
          this[USERLOGIN](res.data.token)
          this.$router.push('/home')
        } else {

          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped lang="less">

  .login_container{
    height: 100%;
    background: url('../assets/img/login2.png') no-repeat;
    background-size: cover;
  }

  .login_footer{
    position: absolute;
    bottom: 0;
    padding-bottom: 0px;
    width: 100%;
    color: rgba(255,255,255,.5);
    margin: 0;
    text-align: center;
    font-size: 14px;
  }

  .login_box{
    width: 320px;
    height: 254px;
    background-color: white;
    border-radius: 3px;
    position: absolute;
    left: 72%;
    top: 50%;
    transform: translate(-50%,-50%);
    border-radius: 4px;
    margin: 0 !important;
    float: right !important;

    .login_nav{
      height: 70px;
      line-height: 70px;
      text-align: center;
      letter-spacing: 5px;
      color: var(--text-color-3);
      font-size: 18px;
      font-weight: 600;

    }
    .login_Form{
      bottom:0px;
      width: 100%;
      padding: 0px 20px;
      box-sizing: border-box;

      .login_btn{
        display: flex;
        justify-content: flex-end;
      }
    }
  }

</style>
